//加密解密包
import AES from '../utils/AES.js'
import 'security'
import 'jsencrypt'

// 加密公钥
const encryptKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJ9s1qlOyv9qpuaTqauW6fUftzE50rVk3yVPZwv1aO1Ch/XSEz76xCwkyvqpaqceRXrPpdBmO5+ruJ+I8osOHo7L5GWEOcMOO+8izp9hXKBBrmRMD4Egpn00k9DhVIEKp/vyddZPS/doxB8onhN6poTJDLdFLFVEicMf52caN9GQIDAQAB";
// 解密私钥
const decryptKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL2KSsvFr9yAtldC6AFAWF3EI9CYRn2P2Fp2trzUyoDGZGRzd4XBE4AeT/7f1+GYPCC2MT/Dhw7pcNpsv+aLlXfuKgHJLVCmi8XxmUmDP9GO9XdMRqe/GW6uJ1eU1N853jm4L35OZHYPNGTmnblTUXvjAXsrYpIa/aDKpm+23Gt/AgMBAAECgYBqtFMdWsKBkZiVkZ4JLk9RIl3DTibJA1UawKBpuCX0zzuvbW3JSAQRaX9BjoT7hPe8trUNH6eGFpeo7/Ys9UIEU61c33Q49NBPEVXH2+PhefIE74b8/H9cu1iYQyn9NSGKt0clo5/CU2G3OA7h+xqD9b7ifd4+DtdrS3KDpTPa0QJBAPhBkdpomvIWmUfI69WerwZHrzMZEFYHThG9SbWw0UgIjdKmSiQmrXawPZLe/o3BxPSIIXDzxiVAimdjVJcfC4kCQQDDc9nUvbLt4TryCfnR7x2KECh3BDEv/cuw/e67m0HYOdooV4KQ4aVous/TbbpcyPGcC180XCHjF11gqVa6hdTHAkBpEJcBsDOjMR093DKy/a1lIwFqxri7L+xCZbHES0jHC5e6BtZp5lSTXpMwjV997vvD4bkFbKX3LhFlIAy0yFbBAkAgvjC43gqypS+9yoQKcldtgKV2wsIGuyq7fN7YmPrf4Vk1tutNoC+YqusUDWbSEmu/a3xIhkK7C3f+MIAyASeTAkEA6CalrJUYBeDcaSHKhmJIceQ7baf8Q7uMYY5gRYuQ4rGudcXXaLi+o2I9f9in5qxE/SM/y9LuBmkzrCuy0tlNXA==";
export default {
    encrypt(data){
        var encrypt = new JSEncrypt();
        //生产16为Key
        var randomStr = AES.generatekey(16)
        //设置公钥
        encrypt.setPublicKey(encryptKey);
        //加密Key
        var encrypted = encrypt.encrypt(randomStr);
        //console.log("加密Key:"+encrypted);
        //加密请求
        var encryptData = JSON.stringify(data);
        var result = AES.encrypt(encryptData,randomStr);
        //console.log(encryptData+" 加密后:"+result);
        return {
            parameter:result,
            decrypt:encrypted
        };
    },
    decrypt(result){
        var decrypt = new JSEncrypt();
        decrypt.setPrivateKey(decryptKey);
        var aesKey = decrypt.decrypt(result.encrypt);
        //console.log("encrypted:"+aesKey);
        // 用aes秘钥进行解密
        var resultData = AES.decrypt(result.data,aesKey);
        //console.log("解密的数据：" + resultData);
        return JSON.parse(resultData);
    }
}